package com;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class Play {
    Backgroud backgroud = new Backgroud();
    Plane plane = new Plane();
    //敌机应该是一个集合
    List<OtherPlane> otherPlanes = new ArrayList<>();
    //战机子弹
    List<Bullet> bullets = new ArrayList<>();
    //敌机子弹
    List<OtherBullet> otherBullets = new ArrayList<>();
    public void draw(Graphics graphics) {
        graphics.setColor(Color.RED);
        graphics.setFont(new Font("楷体",Font.BOLD,30));
        graphics.drawString("游戏界面",200,600);
        //画背景
        backgroud.draw(graphics);
        //画飞机
        plane.draw(graphics);

        //写一个随机数用来生成飞机
        if(((int)(Math.random()*30))==5){
            otherPlanes.add(new OtherPlane());
        }
        //垃圾回收：战机子弹，敌机子弹，敌机
        remove();
        //画所有的敌机
        for(OtherPlane otherPlane: otherPlanes){
            otherPlane.draw(graphics);
        }

        //画战机子弹
        for(Bullet bullet: bullets){
            bullet.draw(graphics);
        }
        //随机生成敌机子弹
        for(OtherPlane otherPlane: otherPlanes){
            if(((int)(Math.random()*20))==5){
                otherBullets.add(new OtherBullet(otherPlane.x,otherPlane.y));
            }
        }
        //画敌机子弹
        for(OtherBullet otherBullet: otherBullets){
            otherBullet.draw(graphics);
        }
        //理解碰撞
        //战机子弹-敌机  碰撞
        for(int i=0;i<bullets.size();i++){
            for(int j=0;j<otherPlanes.size();j++){
                Bullet bullet = bullets.get(i);
                OtherPlane otherPlane = otherPlanes.get(j);
                Rectangle r1 = bullet.getRectangle();
                Rectangle r2 = otherPlane.getRectangle();
                //判断碰撞
                if(r1.intersects(r2)){
                    //移除战机子弹和敌机
                    bullets.remove(i);
                    otherPlanes.remove(j);
                }
            }
        }

        //中弹   战机-敌机子弹
        for(int i=0;i<otherBullets.size();i++){
            OtherBullet otherBullet = otherBullets.get(i);
            Rectangle r1 = otherBullet.getRectangle();
            Rectangle r2 = plane.getRectangle();
            if(r1.intersects(r2)){
                //中弹
                otherBullets.remove(i);
                //生命值减少 life--
//                if(life <= 0){
//
//                }

            }
        }

    }

    public  void remove(){
        //判断资源是否越界，如果越界，清除
        //对遍历中的list进行删除，不能用加强for，只能普通
        for(int i=0;i<otherPlanes.size();i++){
            OtherPlane otherPlane = otherPlanes.get(i);
            if(otherPlane.y>900){
                otherPlanes.remove(i);
            }
        }
        //清除敌机
        //清除战机子弹
    }
    public void keyPressed(KeyEvent e) {
        //将键盘事件又传到飞机里边去
        plane.keyPressed(e);
        //监听开火健
        if(e.getKeyCode() == KeyEvent.VK_SPACE){
            bullets.add(new Bullet(plane.x,plane.y));
        }
    }

    public void keyReleased(KeyEvent e) {
        plane.keyReleased(e);
    }
}
